package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.domain.*;
import com.cleaningcloud.cld.domain.dto.CldStaffContractDto;
import com.cleaningcloud.cld.domain.vo.CldStaffContractVo;
import com.cleaningcloud.cld.mapper.*;
import com.cleaningcloud.cld.service.ICldPayDetailsService;
import com.cleaningcloud.cld.service.ICldStaffContractService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 人事合同Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-22
 */
@Service
public class CldStaffContractServiceImpl implements ICldStaffContractService {
    @Autowired
    private CldStaffContractMapper cldStaffContractMapper;

    @Autowired
    private CldStaffMapper cldStaffMapper;

    @Autowired
    private CldProjectPostMapper projectPostMapper;

    @Autowired
    private CldContractMapper contractMapper;

    @Autowired
    private CldProjectSetMapper projectSetMapper;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    /**
     * 查询人事合同
     *
     * @param staffContractId 人事合同主键
     * @return 人事合同
     */
    @Override
    public CldStaffContract selectCldStaffContractByStaffContractId(Long staffContractId) {
        return cldStaffContractMapper.selectCldStaffContractByStaffContractId(staffContractId);
    }

    /**
     * 查询人事合同列表
     *
     * @param cldStaffContract 人事合同
     * @return 人事合同
     */
    @Override
    public List<CldStaffContractVo> selectCldStaffContractList(CldStaffContractDto cldStaffContract) {
        return cldStaffContractMapper.selectCldStaffContractList(cldStaffContract);
    }

    /**
     * 新增人事合同
     *
     * @param cldStaffContract 人事合同
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldStaffContract(CldStaffContract cldStaffContract) {
        // 查询项目合同信息
        CldContract cldContract = contractMapper.selectCldContractByProjectId(cldStaffContract.getProjectId());
        if (cldContract == null) {
            throw new RuntimeException("该项目暂未签订合同！");
        }
        // 查询员工信息
        CldStaff cldStaff = cldStaffMapper.selectCldStaffByStaffId(cldStaffContract.getStaffId());
        if (cldStaff != null) {
            cldStaff.setStaffStatus(2);
            cldStaff.setPostTime(DateUtils.getNowDate());
            CldProjectPost projectPost = projectPostMapper.selectCldProjectPostByProjectPostId(cldStaffContract.getJobId());
            if (cldStaffContract.isEditPost()) {
                // 修改项目岗位表
                if (projectPost == null) {
                    throw new RuntimeException("项目岗位查询失败！");
                }
                projectPost.setIsMasterDuits(1L);
                projectPost.setStaffId(cldStaff.getStaffId());
                projectPostMapper.updateCldProjectPost(projectPost);
            }
            // 新增人事合同信息
            addStaffContract(cldStaff, cldStaffContract, cldContract);
            // 修改员工状态为已入岗
            cldStaff.setStaffContractId(cldStaffContract.getStaffContractId());
            // 查询项目配置 工服是否为一次性扣款
            CldProjectSet projectSet = projectSetMapper.selectCldProjectSetByProjectId(cldStaffContract.getProjectId());
            if (projectSet != null) {
                CldPayDetails cldPayDetails = new CldPayDetails();
                cldPayDetails.setProjectId(cldStaffContract.getProjectId());
                cldPayDetails.setStaffId(cldStaffContract.getStaffId());
                cldPayDetails.setProjectPostId(projectPost.getProjectPostName());
                cldPayDetails.setIsAlgorithm(2L);
                if (projectSet.getClothesType() != null && projectSet.getClothesType() == 1) {
                    cldPayDetails.setPayType(7L);
                    cldPayDetails.setMoney(projectSet.getClothesMoneyMonth().negate());
                    cldPayDetails.setRemark("工服扣款一次性");
                    cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                }
                if (projectSet.getInsuranceType() != null && projectSet.getInsuranceType() == 1) {
                    cldPayDetails.setPayType(6L);
                    cldPayDetails.setMoney(projectSet.getInsuranceMoneyMonth().negate());
                    cldPayDetails.setRemark("保险扣款一次性");
                    cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                }
            }
        }
        return cldStaffMapper.updateCldStaff(cldStaff);
    }

    private void addStaffContract(CldStaff cldStaff, CldStaffContract cldStaffContract, CldContract cldContract) {
        cldStaffContract.setCreateTime(DateUtils.getNowDate());
        cldStaffContract.setFlag(0L);
        cldStaffContract.setContractStatus(1L);
        cldStaffContract.setContractFile(2L);
        cldStaffContract.setStaffContractNo("HT" + UniqueIdUtils.generateUniqueId());
        cldStaffContract.setStartTime(DateUtils.getNowDate());
        cldStaffContract.setStaffContratName(cldContract.getContractName() + "-" + cldStaff.getStaffName());
        cldStaffContract.setEndTime(cldContract.getEndDate());
        cldStaffContract.setCreateName(SecurityUtils.getUsername());
        cldStaffContract.setCreateId(SecurityUtils.getUserId());
        cldStaffContractMapper.insertCldStaffContract(cldStaffContract);
    }

    /**
     * 修改人事合同
     *
     * @param cldStaffContract 人事合同
     * @return 结果
     */
    @Override
    public int updateCldStaffContract(CldStaffContract cldStaffContract) {
        return cldStaffContractMapper.updateCldStaffContract(cldStaffContract);
    }

    /**
     * 批量删除人事合同
     *
     * @param staffContractIds 需要删除的人事合同主键
     * @return 结果
     */
    @Override
    public int deleteCldStaffContractByStaffContractIds(Long[] staffContractIds) {
        return cldStaffContractMapper.deleteCldStaffContractByStaffContractIds(staffContractIds);
    }

    /**
     * 删除人事合同信息
     *
     * @param staffContractId 人事合同主键
     * @return 结果
     */
    @Override
    public int deleteCldStaffContractByStaffContractId(Long staffContractId) {
        return cldStaffContractMapper.deleteCldStaffContractByStaffContractId(staffContractId);
    }

    @Override
    public List<CldStaffContractStatics> staffContractStatistics(CldStaffContractStatics cldStaffContractStatics) {
        return cldStaffContractMapper.staffContractStatistics(cldStaffContractStatics);
    }
}
